package com.park.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.park.dto.Record;
import com.park.entity.Consume;
import com.park.entity.Park;
import com.park.service.ConsumeService;
import com.park.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RecordController {
    @Autowired
    private ParkService ps;

    @Autowired
    private ConsumeService cs;

    @GetMapping("/record.html")
    public String index() {
        return "record";
    }

    @GetMapping("/record/search")
    @ResponseBody
    public List searchByCarId(@RequestParam(value = "carId", required = false) String carId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
        Page<Park> parkPage = new Page<>();
        parkPage.setSize(pageSize);
        parkPage.setCurrent(page + 1);
        Page<Consume> consumePage = new Page<>();
        consumePage.setSize(pageSize);
        consumePage.setCurrent(page + 1);
        List<Object> ret = new ArrayList<>();
        if (carId == null) {
            List<Park> resPark = ps.list();
            int totalSize = resPark.size();
            resPark = resPark.subList(page * pageSize, Math.min((page + 1) * pageSize, resPark.size()));
            List<Consume> resConsume = cs.list().subList(page * pageSize, (page + 1) * pageSize);
            ret.addAll(assembleRecord(resPark, resConsume));
            ret.add(totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1);
            return ret;
        }
        List<Park> parkList = ps.getByParkCarId(parkPage, carId).getRecords();
        List<Consume> consumeList = cs.list();
        List<Record> res = assembleRecord(parkList, consumeList);
        ret.addAll(res);
        ret.add(parkPage.getTotal() % pageSize == 0 ? parkPage.getTotal() / pageSize : parkPage.getTotal() / pageSize + 1);
        return ret;
    }

    private List<Record> assembleRecord(List<Park> parks, List<Consume> consumes) {
        List<Record> res = new ArrayList<>();
        Map<Integer, Consume> idxConsume = new HashMap<>();
        for (Consume consume : consumes) {
            idxConsume.put(consume.getConsumeId(), consume);
        }
        for (Park park : parks) {
            Consume consume = idxConsume.get(park.getParkId());
            if (consume != null) {
                Record record = new Record();
                record.setParkId(park.getParkId());
                record.setConsumePayAmount(consume.getConsumePayAmount());
                record.setParkCarId(park.getParkCarId());
                record.setParkEnterTime(park.getParkEnterTime());
                record.setParkOutTime(park.getParkOutTime());
                record.setParkPrice(park.getParkPrice());
                record.setParkStatus(park.getParkStatus());
                res.add(record);
            }
        }
        return res;
    }
}
